<?php
class SysAdmin extends Controller {
	function SysAdmin() {
		parent::Controller();
		//$this->load->library('email');
		$this->load->helper("url");
		$this->data["base"] = $this->config->item('base_url');
		$this->load->model('Sysadminmodel');
		//$this->load->model('Mail');
		$this->load->database();	
                session_start();
	}
	var $data;
	function index() {
		if(isset($_SESSION['sys_admin']) and isset($_SESSION['name'])) {
			$this->formReg();
		} else {
			
			//set template
			$this->load->view('layout/head', $this->data);
			$this->load->view('layout/body', $this->data);
			$this->load->view('layout/login-index', $this->data);
			$this->load->view('layout/footer', $this->data);		}
	}
	function formReg() {
		$this->data['title']='Form  User Registration';
		$this->data['dept'] =$this->Sysadminmodel->show_dept();
		$this->data['user_id'] = $this->Sysadminmodel->show_id_user();
		$this->data['form_action'] =site_url('/sysadmin/sysAdmin/formReg_add');
		$this->data['h2_title'] ='';
		
		
				
		$this->load->vars($this->data);
		$this->load->view('layout/head');
		$this->load->view('layout/body');
		$this->load->view('layout/slice-atas');
		$this->load->view('sysadmin/formReg');
		$this->load->view('layout/slice_bawah');
	}
	function formReg_add() {
		$this->data['title']='Form Registration User';
		$this->data['dept'] =$this->Sysadminmodel->show_dept();
		$this->data['user_id'] = $this->Sysadminmodel->show_id_user();
		$this->data['form_action'] =site_url('/sysadmin/sysAdmin/formReg_add');
		$this->data['h2_title'] ='';
		
		//insert log
		if(getenv("HTTP_CLIENT_IP")) { 
			$ipad = getenv("HTTP_CLIENT_IP"); 
		} elseif(getenv("HTTP_X_FORWARDED_FOR")) { 
			$ipad = getenv("HTTP_X_FORWARDED_FOR"); 
		} else { 
			$ipad = getenv("REMOTE_ADDR"); 
		}
		$ipad = substr($ipad, 0,14);
		$translog_id = $this->Sysadminmodel->show_id_trans_log()->row();
		
			$this->xd = $translog_id->id + 1;
		
		
		$this->send_id =$_SESSION['name'];
		$this->tgl = date('Y-m-d H:i:s');
		// set session data 
		$this->data['username'] = $this->input->post('name');
		 $this->data['fname'] = $this->input->post('fname');
		 $this->data['email'] = $this->input->post('email');
		$this->data['pass1'] = $this->input->post('pass');
		 $this->data['pass2'] = $this->input->post('pass2');
		 $this->data['deptid'] = $this->input->post('dept_id');
		$this->data['mandiriID'] = $this->input->post('mandiri_id');
		 $this->data['userRight'] = $this->input->post('user_right');
		 $this->data['status'] = $this->input->post('status');
		 
		// Set validation rules
		$this->form_validation->set_rules('name', 'Name', 'required|callback_valid_name|callback_valid_entry');
		//$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_valid_entry_email');
		$this->form_validation->set_rules('pass', 'Pass', 'required|callback_valid_pass');
		$this->form_validation->set_rules('pass2', 'Pass2', 'required|callback_valid_pass');
		$this->form_validation->set_rules('fname','fname','required|callback_valid_fname');
		$this->form_validation->set_rules('mandiri_id','Mandiri_id','required');
		$this->form_validation->set_rules('user_right','user_right','required');
		
			
		if($this->form_validation->run() == TRUE) {
			
			$insert_user = array('user_id' =>$this->input->post('id'),
									'name' =>$this->input->post('name'),
									'fname' =>$this->input->post('fname'),
									'email' =>$this->input->post('email'),
									'pass' =>md5($this->input->post('pass')),
									'pass2' => $this->input->post('pass2'),
									'dept_id' =>$this->input->post('dept_id'),
									'mandiri_id' =>$this->input->post('mandiri_id'),
									'user_right' =>$this->input->post('user_right'),
									'status' =>$this->input->post('status'));
		//insert data ke user
			$this->Sysadminmodel->insert_user($insert_user);
			
			$insert_log = array('id' => $this->xd,
							'user_id' =>$this->send_id,
							'ticket_id' =>$this->input->post(''),
							'notes' =>('Add New User'),
							'datetime' =>$this->tgl,
							'ip' =>$ipad);
		//insert ke table trans log
		$this->Sysadminmodel->insert_log($insert_log);
		
		/*----------
		send mail konfirmasi
		------------
		$this->Mail->config_mail();
		$this->Mail->send_by_ticket();
		/*----------
		END send mail konfirmasi
		------------*/
			
			$this->session->set_flashdata('message', 'berhasil tambah user !');
			
			redirect('sysadmin/sysAdmin');
		}else {
			
		}
		/*}*/
		
		$this->load->vars($this->data);
		$this->load->view('layout/head');
		$this->load->view('layout/body');
		$this->load->view('layout/slice-atas');
		$this->load->view('sysadmin/formReg');
		$this->load->view('layout/slice_bawah');
	}
	
	
	
	function valid_pass() {
		if($this->input->post('pass') == $this->input->post('pass2')) {
			return TRUE;
		} else {
			$this->form_validation->set_message('valid_pass', ' password harus sama ' );
			return FALSE;
		}
	}
	
	function valid_entry()
	{
		$name 		= $this->input->post('name');
		
		
		if($this->Sysadminmodel->valid_entry($name) == FALSE)
		{
			$this->form_validation->set_message('valid_entry', 'Username sudah ada ' );
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	function valid_entry_email()
	{
		$mail		= $this->input->post('email');
		
		
		if($this->Sysadminmodel->cek_mail($mail) == FALSE)
		{
			$this->form_validation->set_message('valid_entry_email', 'Email sudah terdaftar ' );
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	function valid_name() {
		if(preg_match("/^[0-9a-zA-z]+$/",$this->input->post('name')))
		{
			return true;
			
			
		} else {
			$this->form_validation->set_message('valid_name', 'Username hanya boleh huruf atau angka ' );
			return false;
		}
		
	}
	function valid_fname() {
		if(preg_match("/^[0-9a-zA-z]+$/",$this->input->post('fname')))
		{
			return true;
			
			
		} else {
			$this->form_validation->set_message('valid_fname', 'Full Name hanya boleh huruf atau angka ' );
			return false;
		}
		
	}
	
	/*function valid_name()
	{
		$vn = $this->input->post('name');
		if ($this->input->xss_clean($vn, false) === true)
		{
			$this->form_validation->set_message('valid_name', 'cek ' );
					//echo "<script>alert('<h1>TEST</h1>);</script>";

			return true ;
		}
	}	*/




	
}
?>